package com.maimang.platform.config;

import com.maimang.platform.common.config.Global;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname ResourceConfig
 * @description 通用配置
 * @date 2019-04-23 23:49
 * @email jintaozhao@qq.com
 */
@Configuration
public class ResourceConfig implements WebMvcConfigurer {

	@Value("${shiro.user.indexUrl}")
	private String indexUrl;

	/**
	 * 设置默认首页，当输入域名是可以自动跳转到默认指定的网页
	 *
	 * @param registry
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("forward:" + indexUrl);
	}

	/**
	 * 文件资源默认配置
	 *
	 * @param registry
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 文件上传路径
		registry.addResourceHandler("/profile/**")
				.addResourceLocations("file:" + Global.getProfile());
		// swagger配置
		registry.addResourceHandler("swagger-ui.html")
				.addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**")
				.addResourceLocations("classpath:/META-INF/resources/webjars/");
		// 添加静态资源访问
		// registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		registry.addResourceHandler("/swagger-resources/**")
				.addResourceLocations("classpath:/META-INF/resources/swagger-resources/");

		registry.addResourceHandler("/swagger/**")
				.addResourceLocations("classpath:/META-INF/resources/swagger*");

		registry.addResourceHandler("/v2/api-docs/**")
				.addResourceLocations("classpath:/META-INF/resources/v2/api-docs/");
	}
}
